<?php

if (!defined('IN_IA')) {
    die( 'Access Denied' );
}

class shipment {

    public $size = 20;
    public $page = 1;

    public $pk = 'id';
    public $where = 1;
    public $field = '';

    public $tableOrder = 'ewei_shop_order';
    public $tableGoods = 'ewei_shop_goods';
    public $tableOrderGoods = 'ewei_shop_order_goods';
    public $tableMemberAddress = 'ewei_shop_member_address';

    public function setPage($page){
        $this->page = $page;
    }

    public function setSize($size){
        $this->size = $size;
    }

    public function setPk($pk){
        $this->pk = $pk;
    }

    public function where($where){

        $where = $this->_whereFilter($where);

        $this->where = $where;
        return $this;
    }

    public function field($field){

        if(is_array($field) && !empty($field)){
            $field = implode(',', $field);
        }else{
            $field = '*';
        }

        $this->field = $field;
        return $this;
    }

    /**
     * 获取订单数据
     * @param bool $pagination  是否需要分页
     * @return array
     */
    public function getOrderLists($pagination = true){
        $result = array();

        if($pagination){
            $resultSql = 'SELECT '.$this->field.' FROM '.tablename($this->tableOrder).' WHERE '.$this->where.' LIMIT '. ($this->page - 1) * $this->size .' , '. $this->size;
            $countSql = 'SELECT count(id) FROM '.tablename($this->tableOrder).' WHERE '.$this->where;
            $count = pdo_fetchcolumn($countSql,array());

            $result['pagination'] = pagination($count, $this->page, $this->size);
            $result['result'] = pdo_fetchall($resultSql, array(), $this->pk);
        }else{
            $resultSql = 'SELECT '.$this->field.' FROM '.tablename($this->tableOrder).' WHERE '.$this->where;
            $result = pdo_fetchall($resultSql, array(), $this->pk);
        }

        return $result;
    }

    /**
     * 根据订单号，获取商品总数。
     * @param $orderids
     * @return bool|int
     */
    public function getGoodsSumByOrderIds($orderids){
        global $_W;

        $goodsTotal = 0;

        $sql  = ' SELECT sum(g.total) FROM '.tablename($this->tableOrder).' AS o ';
        $sql .= ' LEFT JOIN '.tablename($this->tableOrderGoods).' AS g ON o.`id` = g.`orderid`';

        if(!is_array($orderids) && intval($orderids) != 0){

            $sql .= ' WHERE o.`id` = :orderid AND o.`uniacid` = :uniacid ';
            $params = array(':uniacid' => $_W['uniacid'], ':orderid' => $orderids);

            $goodsTotal = pdo_fetchcolumn($sql, $params);

        }else if(!empty($orderids)){

            $sql .= ' WHERE o.`id` IN ( '.implode(',', $orderids).' ) AND o.`uniacid` = :uniacid ';
            $params = array(':uniacid' => $_W['uniacid']);


            $goodsTotal = pdo_fetchcolumn($sql, $params);
        }

        return $goodsTotal;
    }

    /**
     * 根据订单号，获取商品详情。
     * @param $orderids
     * @return bool | array
     */
    public function getGoodsByOrderIds($orderids){
        global $_W;

        $result = array();

        $sql  = ' SELECT g.id, g.orderid, g.goodsid, g.total, o.refundid, o.ordersn  FROM '.tablename($this->tableOrder).' AS o ';
        $sql .= ' LEFT JOIN '.tablename($this->tableOrderGoods).' AS g ON o.`id` = g.`orderid`';

        if(!is_array($orderids) && intval($orderids) != 0){

            $sql .= ' WHERE o.`id` = :orderid AND o.`uniacid` = :uniacid ';
            $params = array(':uniacid' => $_W['uniacid'], ':orderid' => $orderids);

            $result = pdo_fetchall($sql, $params);

        }else if(!empty($orderids)){

            $sql .= ' WHERE o.`id` IN ( '.implode(',', $orderids).' ) AND o.`uniacid` = :uniacid ';
            $params = array(':uniacid' => $_W['uniacid']);

            $result = pdo_fetchall($sql, $params);
        }

        return $result;
    }

    public function getSendInfoByOrderIds($orderids){
        global $_W;

        $result = array();

        $field = 'o.id, o.ordersn,o.price, o.remark,og.total, og.price goodsprice,g.id as goodsid, g.title, g.weight,g.type, a.realname, a.realname, a.mobile, a.province, a.city, a.area, a.address';

        $sql  = ' SELECT '.$field.' FROM '.tablename($this->tableOrder).' AS o ';
        $sql .= ' LEFT JOIN '.tablename($this->tableOrderGoods).' AS og ON o.`id` = og.`orderid` ';
        $sql .= ' LEFT JOIN '.tablename($this->tableGoods).' AS g ON og.`goodsid` = g.`id` ';
        $sql .= ' LEFT JOIN '.tablename($this->tableMemberAddress).' AS a ON o.`addressid` = a.`id` ';

        if(!is_array($orderids) && intval($orderids) != 0){

            $sql .= ' WHERE o.`id` = :orderid AND o.`uniacid` = :uniacid ';
            $params = array(':uniacid' => $_W['uniacid'], ':orderid' => $orderids);

            $result = pdo_fetchall($sql, $params);

        }else if(!empty($orderids)){

            $sql .= ' WHERE o.`id` IN ( '.implode(',', $orderids).' ) AND o.`uniacid` = :uniacid ';
            $params = array(':uniacid' => $_W['uniacid']);

            $result = pdo_fetchall($sql, $params);
        }

        return $result;
    }

    /**
     * 查询条件过滤器
     * @param $where
     * @return string
     */
    private function _whereFilter($where){
        global $_W;

        $result = ' `uniacid` = '. $_W['uniacid'];

        if(!empty($where)){
            foreach($where as $key => $val){
                if(is_array($val)){
                    foreach($val as $k => $v){
                        $result .= $this->_fieldOperation($key, array($k => $v));
                    }
                }
            }
        }

        return $result;
    }

    /**
     * 查询条件重组
     * @param $key
     * @param $field
     * @return string
     */
    private function _fieldOperation($key, $field){

        $operator = strtolower(key($field));

        switch ($operator) {
            case 'eq' :
                $result = ' AND `' . $key . '` = "' . end($field) . '" ';
                break;
            case 'neq' :
                $result = ' AND `' . $key . '` != "' . end($field) . '" ';
                break;
            case 'gt' :
                $result = ' AND `' . $key . '` > "' . end($field) . '" ';
                break;
            case 'egt' :
                $result = ' AND `' . $key . '` >= "' . end($field) . '" ';
                break;
            case 'lt' :
                $result = ' AND `' . $key . '` < "' . end($field) . '" ';
                break;
            case 'elt' :
                $result = ' AND `' . $key . '` <= "' . end($field) . '" ';
                break;
            case 'like' :
                $result = ' AND `' . $key . '` like "%' . end($field) . '%" ';
                break;
            default :
                $result = '';
                break;
        }


        return $result;
    }

}